package bytecode;

import java.util.HashMap;
import java.util.Map;

/**
 * Vycet instrukci, se kterymi umi interpreter pracovat
 * @author honzee
 */
public enum InstructionToken {

    MAIN(0x11),
    STACK(0x12),
    LOCALS(0x13),
    
    INVOKE(0x22),

    LDS(0x30),
    LDC(0x31),
    STORE(0x32),
    LOAD(0x33),
    ASTORE(0x34),
    ALOAD(0x35),
    NEWARRAY(0x36),
    DEALLOC(0x37),

    IADD(0x41),
    SADD(0x42),
    ISUB(0x43),
    IMUL(0x44),
    IDIV(0x45),
    
    IF(0x51),
    NE(0x52),
    EQ(0x53),
    GE(0x54),
    LE(0x55),
    GT(0x56),
    LT(0x57),

    OF(0x61),
    CF(0x62),

    WRITE(0x73),
    WRITELN(0x74),
    IREAD(0x75),

    GOTO(0x81),
    IRETURN(0x82),
    RETURN(0x83),
    END(0x84),

    CONVERT(0x91),

    VALUE;

    private int byteValue;

    private static Map<Integer, InstructionToken> tokenMap;

    private InstructionToken() {}

    private InstructionToken(int byteValue){
        this.byteValue = byteValue;
    }

    public static InstructionToken getToken (int key){
        if (tokenMap == null) {
            initTokenMap();
        }

        if (tokenMap.containsKey(key)) {
            return tokenMap.get(key);
        }
        return VALUE;
    }

    private static void initTokenMap() {
        tokenMap = new HashMap<Integer, InstructionToken>();
        for (InstructionToken i : values()) {
            tokenMap.put(i.byteValue, i);
        }
    }
}
